<?php


namespace app\admin\model;
use think\Db;
use think\Model;

class User
{
    protected $table='hb_user';

    public function getALL()
    {
//        $result=Db::table('hb_user')->select();
//        return $result;

        //        查询所有的用户信息
        $userlist=Db::table('hb_user')
            ->field(['id','username'])
            ->select();
//        dump($userlist);


        $arr=$userlist;
        $test=null;

        foreach ($userlist as $c=>$v) {
//          查询对应用户的角色信息
            $rolelist = Db::table(['hb_role' => 'r', 'user_role' => 'ur'])
                ->field(['r.rolename', 'ur.u_id'])
                ->where('r.id=ur.r_id')
                ->where('ur.u_id', $v['id'])
                ->select();
//            dump($rolelist);

            foreach ($rolelist as $k=>$v)
            {
                if ($test==$v['u_id']){
                    array_push($a,$v['rolename']);

                }else{
                    $test=$v['u_id'];
                    $a=array($v['rolename']);
                }
//                拼凑出需要的数组
                $arr[$c]['rolename']=$a;
            }
        }
        return $arr;
    }


    public  function  save($p)
    {
        $pass=hash('sha1',$p['pass']);

        $p['pass']=$pass;
        dump($p);
        $id = Db::table('hb_user')
            ->insertGetId($p);
        $a=[
            'u_id'=>$id,
            'r_id'=>6,
        ];

        $result=Db::table('user_role')
            ->insert($a);
        return $result;
    }











}